package com.qf.dao.impl;

import com.qf.dao.AdminDao;
import com.qf.pojo.Admin;
import com.qf.pojo.User;
import com.qf.utils.DruidJdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 高圆圆
 * @date 2023/5/24 16:43
 * 管理员数据访问接口实现
 */
public class AdminDaoImpl  implements AdminDao {
    /**
     * 数据访问接口 获取管理员实体
     * @param username 传过来的用户名
     * @return 返回实体
     */
    @Override
    public Admin selectAdminByUsername(String username) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from admin where username = ?" ;
        //执行查询
        Admin admin = qr.query(sql, new BeanHandler<>(Admin.class), username);
        return admin;
    }

    /**
     * 数据访问接口 获取前台所有用户
     * @return返回 用户列表
     */
    @Override
    public List<User> selectUsers() throws SQLException {
        //执行器对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user " ;
        //执行
        List<User> list = qr.query(sql, new BeanListHandler<>(User.class));
        return list;
    }

    /**
     * 数据访问接口获取前台指定的用户
     * @param uid 用户uid
     * @return 返回用户实体
     */
    @Override
    public User selectUserByUid(String uid) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where uid = ?" ;
        //执行查询
        User user = qr.query(sql, new BeanHandler<>(User.class), uid);
        return user;
    }

    /**
     * 数据访问接口更新用户
     * @param user 用户实体
     * @return 返回影响的行数
     */
    @Override
    public int updateUser(User user) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "update user set username = ?,password=?,name= ?,email=?, sex=? where uid =?" ;
        //执行更新
        int count = qr.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getSex(),
                user.getUid());
        return count;
    }


    /**
     * 数据访问接口 删除指定的用户
     * @param uid 用户id
     * @return 返回影响的行数
     */
    @Override
    public int deleteUser(String uid) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "delete  from  user where uid = ? " ;
        //更新
        int count = qr.update(sql, uid);
        return count;
    }

    /**
     * 查询用户总记录数
     * @return 返回总条数
     */
    @Override
    public int getTotalCount() throws SQLException {
        //QueryRunner执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select count(uid) from user" ;
        //查询的单行单例数据,存储在ResultsetHandler的子实现类ScalarHandler
        Object obj = qr.query(sql, new ScalarHandler<>());
        int totalCount = Integer.parseInt(String.valueOf(obj));
        return totalCount;
    }

    /**
     * 分页查询用户
     * @param currentPage 当前页码
     * @param pageSize     每页显示的条数
     * @return  返回分页后查询的列表
     */
    @Override
    public List<User> getUserByPage(int currentPage, int pageSize) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user limit ?,?" ;
        //查询的多条记录封装到List
        List<User> list = qr.query(sql, new BeanListHandler<>(User.class), (currentPage - 1) * pageSize, pageSize);
        return list;
    }
}
